// pages/zhichu/zhichu.js
const App = getApp()
import {colors} from '../../utils/constData'
const date = new Date()
import {
  listAll,
  dateCharts,
  detailList,
  lineChart,
  shareAllPay
} from '../../utils/api/zhichu'
import {time} from '../../utils/util.js'
Component({
  options: {
    addGlobalClass: true
  },
  /**
   * 页面的初始数据
   */
  data: {
    color: colors[0],
    //总流水列表
    zongLiuShui: [],
    zongLiuShuiTime:'',
    // 分享数据
    shareAllPayList: [],
    shareAllPayOtherList: [],
    //charts图表
    dateYear: date.getFullYear(),
    dateYearMonth: date.getFullYear() + '-' + (date.getMonth() + 1),
    // 年总支出
    yearPayAll: 0,
    //月支出
    monthPayAll: 0,
    // 年echarts
    yearCharts: [],
    yeartop5List: [],
    yearXLine: [],
    yearYLine: [],
    // 年月echarts
    monthCharts: [],
    monthtop5List: [],
    monthXLine: [],
    monthYLine: [],
    //最近记录
    zuiJinJl: [],
    //是否弹窗
    show: false,
  },
  pageLifetimes: {
    show() {
      this.updataFn()
    }
  },
  methods: {
    // 更新数据
    updataFn() {
      //总流水
      this.getList();
      this.shareAllPay()
      // 年支出
      this.getYearChat();
      this.getYearChatLine();
      // 月支出
      this.getMonthChat();
      // 最近记录
      this.detailList();
    },
    // 初始获取数据--------------
    /**总流水 */
    async getList() {
      const res = await listAll({})
      this.setData({
        zongLiuShui: res.list,
        zongLiuShuiTime:res.time
      })
    },
    async shareAllPay() {
      const res = await shareAllPay()
      let list = res.list
      list.map(item => {
        const obj = item
        item.list = Object.values(obj)[0]
        item.name = Object.keys(obj)[0]
        item[Object.keys(obj)[0]] = undefined
      })
      this.setData({
        shareAllPayList: res.sum,
        shareAllPayOtherList: list
      })
    },
    /**年度echat*/
    async getYearChat() {
      const res = await dateCharts({
        'date': this.data.dateYear,
        'dateType': 'y'
      })
      this.setData({
        yearCharts: res.list,
        yearPayAll: res.sum,
        yeartop5List: res.topList
      })
    },
    async getYearChatLine() {
      const res = await lineChart()
      const yearList = res.yearLineChart
      const monthList = res.monthLineChart
      let yearXLine = [],
        yearYLine = [],
        monthXLine = [],
        monthYLine = []
      yearList.forEach(item => {
        yearXLine.push(item.years)
        yearYLine.push(item.sum)
      })
      monthList.forEach(item => {
        monthXLine.push(item.yearMonth)
        monthYLine.push(item.sum)
      })
      this.setData({
        yearXLine,
        yearYLine,
        monthXLine,
        monthYLine
      })
    },
    /**年月echart */
    async getMonthChat() {
      const res = await dateCharts({
        'date': this.data.dateYearMonth,
        'dateType': 'm'
      })
      this.setData({
        monthCharts: res.list,
        monthPayAll: res.sum,
        monthtop5List: res.topList
      })
    },
    /**明细数据 */
    async detailList() {
      const res = await detailList({})
      res.map(ite=>{
        ite.list.map(item=>item.time=time(item.time))
      })
      this.setData({
        zuiJinJl: res
      })
    },

    /**页面按钮事件------------------------ */
    /**
     * 日期选择器year返回数据 
     */
    confirmYearDate(e) {
      this.setData({
        dateYear: e.detail
      })
      this.getYearChat();
    },
    /**
     * 日期选择器month返回数据 
     */
    confirmMonthDate(e) {
      this.setData({
        dateYearMonth: e.detail
      })
      this.getMonthChat();
    },
    /**跳转新增最近记录 */
    add: function () {
      wx.navigateTo({
        url: '/pages/addzhichu/index'
      });
    },
    /**删除最近记录某一条数据 */
    del(e) {
      const {
        index,
        pageIndex
      } = e.detail
      let zuiJinJl = this.data.zuiJinJl
      zuiJinJl.map((ite, ind) => {
        if (ind === pageIndex) ite.list.splice(index, 1)
      })
      this.setData({
        zuiJinJl
      })
    },
    /**编辑 */
    edit(e) {
      const id = e.detail.id
      wx.navigateTo({
        url: '/pages/addzhichu/index?id=' + id
      });
    },
    // 页面交互---------------------------------------
    /**点击分享 触发弹框*/
    showPopup() {
      if(!this.data.shareAllPayOtherList.length){
        return
      }
      this.setData({
        show: true
      });
    },
    /**点击关闭分享弹框 */
    onClose() {
      this.setData({
        show: false
      });
    },
  }
})